package umputun.jutw.Process.from_twi;

import umputun.jutw.utils.logging.ULogger;

import java.util.ArrayList;
import java.util.List;

/*
 Copyright 2010 Umputun

 This file is part of JUTW.

 JUTW is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 JUTW is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with JUTW.  If not, see http://www.gnu.org/licenses/.
*/

final class Translator {

	public static String translate(String input) {
		List<String> tags = extractTags(input);

		String[] elems = input.split(" ");
		StringBuilder bld = new StringBuilder();

		for (String tag: tags) {
			bld.append(tag).append(' ');
		}

		for (int i=0; i<elems.length; i++) {
			bld.append(processWord(elems[i])).append(' ');
		}

		return bld.toString();
	}

	private static List<String> extractTags(String input) {
		List<String> result = new ArrayList<String>();
		String[] elems = input.split(" ");
		for (String s : elems) {
			if (s.startsWith("#"))
				result.add("*"+s.substring(1));
		}
		return result;
	}


	private static String processWord(String word) {

		String result = word;

		//twitter name
		if (result.startsWith("@")) {
			String suffix = "";
			if (result.endsWith(":") || result.endsWith(",") || result.endsWith(".") || result.endsWith("-")
					|| result.endsWith("?") || result.endsWith("!") ) {
				suffix = " " + result.substring(result.length()-1, result.length());
				result = result.substring(0, result.length()-1);
			}
			result = result + " http://twitter.com/" + result.substring(1) + suffix;
		}

		if (result.contains("&gt;")) result = result.replace("&gt;",">");
		if (result.contains("&lt;")) result = result.replace("&lt;","<");
		if (result.contains("&quot;")) result = result.replace("&quot;","\"");
		if (result.contains("&amp;")) result = result.replace("&amp;","&");
		if (result.contains("&#39;")) result = result.replace("&#39;","'");

		//process juick-specific links
		boolean is_juick_link = false;
		String ju_prefix = "http://juick.com/";
		if (result.startsWith("http://juick.com/")) {
			is_juick_link = true;
			ju_prefix = "http://juick.com/";
		}
		if (result.startsWith("juick.com/")) {
			is_juick_link = true;
			ju_prefix = "juick.com/";
		}

		if (is_juick_link) {
			String possible_name = 	result.substring(ju_prefix.length());
			if (possible_name.length()>1 && possible_name.charAt(0)>'9' && !possible_name.equals("last") && !possible_name.contains("?tag") && !possible_name.contains("/")) {
				result = "@" + possible_name;
			}
		}


		if (!result.equals(word))
			log.finest("replaced: " + word + "->" + result);
		return result;
	}

	private static final ULogger log = ULogger.getLogger();
}
